package talk.dapter;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.talk.debby.talk.R;

import java.util.List;

import talk.Bean.Choice;
import talk.listener.ChoiceListener;

/**
 * Created by debby on 2018/5/31.
 * 描述:
 */

public class ChoiceAdapter extends RecyclerView.Adapter<ChoiceAdapter.ViewHolder> {

    private Activity activity;
    private List<Choice.Option> optionList;
    private ChoiceListener listener;
    private int lastChoose = -1;


    public ChoiceAdapter(Activity activity, List<Choice.Option> optionList, ChoiceListener listener) {
        this.activity = activity;
        this.optionList = optionList;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(activity).inflate(R.layout.adapter_choose, parent, false);
        return new ViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.option.setText(optionList.get(position).content);

        holder.option.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lastChoose = position;
                listener.onItemClick(position);
                notifyDataSetChanged();
            }
        });
        if (lastChoose == position) {
            setChoose(holder.option, true);
        } else {
            setChoose(holder.option, false);
        }
    }

    @Override
    public int getItemCount() {
        return optionList == null ? 0 : optionList.size();
    }

    //设置选中效果
    private void setChoose(TextView option, Boolean choose) {
        if (choose) {
            option.setTextColor(Color.WHITE);
            option.setBackground(activity.getResources().getDrawable(R.drawable.shape_round_blue));
        } else {
            option.setTextColor(activity.getResources().getColor(R.color.blue));
            option.setBackground(activity.getResources().getDrawable(R.drawable.shape_round_white));
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView option;

        public ViewHolder(View itemView) {
            super(itemView);
            option = (TextView) itemView.findViewById(R.id.tv_option);
        }
    }

}
